package com.orangelife.mobile.housepurchasing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.PhoneCallUtil;
import com.curry.orangelife.mobile.R;

import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;

import com.orangelife.mobile.housepurchasing.biz.ToEntrustHttpBiz;

import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by HeYuCan on 2016/5/18.
 */
public class ToEntrustActivity extends OrangeLifeBaseActivity {
    @Bind(R.id.tv_to_entrust)
    TextView tvToEntrust;
    @Bind(R.id.flback)
    FrameLayout flBack;
    @Bind(R.id.tv_call)
    TextView tvCall;
    @Bind(R.id.rl_entrust_success)
    RelativeLayout rlEntrustSuccess;
    /*委托成功时的遮罩*/
    @Bind(R.id.rl_mask)
    RelativeLayout rlMask;
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> entrustFeedback = new HashMap<>();
    /*小区的委托热线*/
    private String commHotline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_entrust);
        ButterKnife.bind(this);
        getCommHotline();

    }


    /**
     * 获取小区委托热线
     */
    private void getCommHotline() {
        ToEntrustHttpBiz.requestCommHotLine(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                map = ToEntrustHttpBiz.getCommHotline(response);
                commHotline = (String) map.get("hotline");
            }
        });

    }





    @OnClick({R.id.tv_call,R.id.tv_to_entrust,R.id.iv_title_back,R.id.rl_entrust_success,R.id.rl_mask})
    public void onClick(View view){

        switch (view.getId()) {
            case R.id.tv_call:
                    /*打电话*/
                PhoneCallUtil.getInstances().callPhone(ToEntrustActivity.this, commHotline, getResources().getString(R.string.comm_hotline));
                break;
            case R.id.tv_to_entrust:
                Intent intent1 = getIntent();
                String commID = intent1.getStringExtra("commID");
                ToEntrustHttpBiz.postEntrust(commID, new VolleyHttpUtil.ResponseCallBack() {
                    @Override
                    public void handleResponse(JSONObject response, int errCode) {
                        entrustFeedback = ToEntrustHttpBiz.getEntrustFeedback(response);
                        String feedback = String.valueOf(entrustFeedback.get("errInfo"));
                        if(OrangeErrorHandler.getInstance().isSuccess(response))
                            if (feedback.equals("OK")) {
                                rlMask.setVisibility(View.VISIBLE);
                                rlEntrustSuccess.setVisibility(View.VISIBLE);
                            }
                    }
                });

                break;
            case R.id.iv_title_back:

                finish();

                break;
            case R.id.rl_entrust_success:
                rlEntrustSuccess.setVisibility(View.GONE);
                rlMask.setVisibility(View.GONE);
                break;
            case R.id.rl_mask:
                rlEntrustSuccess.setVisibility(View.GONE);
                rlMask.setVisibility(View.GONE);
            default:
                break;
        }
    }

}
